package com.lee.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lee.bean.Follow;
import com.lee.bean.Refer;
import com.lee.mapper.FollowMapper;
import com.lee.mapper.ReferMapper;
import com.lee.service.ReferService;
import com.lee.util.AjaxResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by dllo on 18/4/17.
 */
@Service
public class ReferServiceImpl implements ReferService {

    @Resource
    private ReferMapper referMapper;
    @Resource
    private FollowMapper followMapper;

    @Override
    public PageInfo queryReferByPage(Integer pageNo, Integer pageSize,String status) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 10 : pageSize;

        //这句话一定写在查询之前
        PageHelper.startPage(pageNo, pageSize);
        List<Refer> referList = referMapper.selectAllReferByRefer(status);
        return new PageInfo<Refer>(referList);
    }

    @Override
    public AjaxResult selectRefer(String rid) {
        AjaxResult result = new AjaxResult();
        Refer refer = referMapper.selectByPrimaryKey(rid);
        if (refer == null) {
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(refer);
        return result;
    }

    @Override
    public AjaxResult selectAllFollow(String referid) {
        AjaxResult result = new AjaxResult();
        List<Follow> followList = followMapper.selectAllFollow(referid);
        if (followList == null) {
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(followList);
        return result;
    }

    @Override
    public AjaxResult addFollow(Follow follow) {
        AjaxResult result = new AjaxResult();
        int num = followMapper.insertSelective(follow);
        if (num < 1) {
            result.setCode("0");
            result.setMessage("添加失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("添加成功");
        return result;
    }

    @Override
    public AjaxResult editRefer(Refer refer) {
        AjaxResult result = new AjaxResult();
        int num = referMapper.updateByPrimaryKeySelective(refer);
        if (num < 1) {
            result.setCode("0");
            result.setMessage("修改失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("修改成功");
        return result;
    }
}
